﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using Dapper;
using Dapper.Contrib.Extensions;
using System.Linq;

namespace Tauren.Business.DLL.TenantInformation
{
    /// <summary>
    /// 租户操作数据库层
    /// </summary>
    public class TenantInformationBusiness : ITenantInformationBusiness
    {
        public TenantInformationBusiness(IDbConnection dbConnection)
        {
            this.dbConnection = dbConnection;
        }

        public IDbConnection dbConnection { get; set; }

        /// <summary>
        /// 新增租户
        /// </summary>
        /// <param name="tenant"></param>
        /// <returns></returns>
        public long Add(Entity.Model.TenantInformation tenant) => dbConnection.Insert<Entity.Model.TenantInformation>(tenant);

        /// <summary>
        /// 获取所有的租户信息
        /// </summary>
        /// <returns></returns>
        public List<Entity.Model.TenantInformation> All() => dbConnection.GetAll<Entity.Model.TenantInformation>().ToList();

        /// <summary>
        /// 删除租户
        /// </summary>
        /// <param name="auotId"></param>
        /// <returns></returns>
        public bool Delete(int auotId) => dbConnection.Execute($"delete from TenantInformation where autoID={auotId}") >= 0;

        /// <summary>
        /// 删除租户--根据租户ID
        /// </summary>
        /// <param name="auotId"></param>
        /// <returns></returns>

        public bool DeleteByCode(string code) => dbConnection.Execute($"delete from TenantInformation where code={code}") >= 0;

        /// <summary>
        /// 编辑租户
        /// </summary>
        /// <param name="tenant"></param>
        /// <returns></returns>
        public bool Editor(Entity.Model.TenantInformation tenant) => dbConnection.Update<Entity.Model.TenantInformation>(tenant);

        /// <summary>
        /// 根据主键获取数据
        /// </summary>
        /// <param name="autoId"></param>
        /// <returns></returns>
        public Entity.Model.TenantInformation Get(int autoId) => dbConnection.Get<Entity.Model.TenantInformation>(autoId);

        /// <summary>
        /// 根据编码获取数据
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>

        public Entity.Model.TenantInformation GetByCode(string code) => dbConnection.Query<Entity.Model.TenantInformation>("SELECT * FROM TenantInformation WHERE code=@code", new Entity.Model.TenantInformation() { Code = code }).AsList().FirstOrDefault(); 
    }
}
